package org.example.juc;

import jdk.nashorn.internal.runtime.JSONListAdapter;

/**
 * @author 张琪
 * @date 2024/4/18 14:08
 */
public class JoinExample {

    public static void main(String[] args) {
        JoinExample joinExample = new JoinExample();
        joinExample.test();
    }

    private class A extends Thread {
        @Override
        public void run() {
            System.out.println("A");
        }
    }

    private class B extends Thread {
        private A a;

        public B(A a) {
            this.a = a;
        }

        @Override
        public void run() {
            try {
                // 使用join 方法, 会挂起当前线程, 知道a线程执行完毕; 因此结果打印的是 A B
                a.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("B");
        }
    }

    public void test() {
        A a = new A();
        B b = new B(a);
        b.start();
        a.start();
    }

}
